#!/bin/bash
. ./var
newvitrulhost(){
    #VM虚拟机克隆安装配置项。
    #默认删除网卡信息，配置静态IP，更改主机名,安装zabbixagent。
    #只需配置一次，用完即可删除本文档。
    echoRed "----------------------------------------------------"
    [ -e /etc/sysconfig/network-scripts/ifcfg-eth0 ] &&  echoRed "请勿 重复执行……" && exit 1

    #NetworkManager is not running，需要启动NewworkManager才能使用nmcli, systemctl status NetworkManager
    hostnameip=$(hostname -I)
    #因为我装了docker，所有有很多ip，这里我用awk取的第一个ip
    #hostnameip=$(hostname -I | awk -F' ' '{print $1}')
    int=$(ls /etc/sysconfig/network-scripts/ifcfg-*   | grep -v lo)
    eth=$(nmcli dev status | grep connected | awk '{print $1}')
    hostname=$(echo "ALD-"$(echo $hostnameip|awk -F'.' '{print $4}')"-SERVER")

    #如果没有安装zabbix-agent,那么就安装agent。在前面定义了zabbix_server
    #https://mirrors.tuna.tsinghua.edu.cn/zabbix/zabbix/4.4/rhel/7/x86_64/zabbix-agent-4.4.6-1.el7.x86_64.rpm
    sleep 2
    /usr/sbin/zabbix_agentd -V &> /dev/null
    if [ $? = 0 ];then
        echo "已经安装了zabbix_agentd"
    else
        rpm -ivh http://mirrors.aliyun.com/zabbix/zabbix/4.4/rhel/7/x86_64/zabbix-release-4.4-1.el7.noarch.rpm 
       	yum install zabbix-agent -y
    fi
    #/usr/sbin/zabbix_agentd -V &> /dev/null || (rpm -ivh http://mirrors.aliyun.com/zabbix/zabbix/4.4/rhel/7/x86_64/zabbix-release-4.4-1.el7.noarch.rpm && yum install -y zabbix-agent)
    echo "当前IP地址： "[$hostnameip]
    echo "网卡名称：   "${eth}
    echo "网络文件路径： "$int
    echo "当前主机名："$(hostname)
    echo "新主机名为："$hostname
    echo "zabbix服务器为：["$zabbix_server]
    echo "----------------------------------------------------"
    echo "即将进行相关操作"
    echo "    删除克隆的网卡信息、配置静态IP、更改主机名、修改zabbixAgent"
    echo "----------------------------------------------------"
    sleep 3
    #read -n 1 -t 30  -p "是否进行修改?y/n" Number
    #echo
    #case $Number in
    #[Yy])
    echo "----------------------------------------------------"
    echo "    (1) 删除克隆的网卡信息"
    [ -f /etc/udev/rules.d/*.rules ] && rm -f /etc/udev/rules.d/*.rules
    sed -i 's/UUID/#UUID/g'   $int
    grep ^DEVICE  $int &&   sed -i "s/^DEVICE.*/DEVICE=$eth/g"   $int ||echo "DEVICE="$eth  >>  $int
    cat $int | grep UUID
    cat $int  | grep DEVICE
    #配置静态IP
    echo "----------------------------------------------------"
    echo "    (2) 配置静态IP"
    sed -i 's/^BOOTPROTO=.*/BOOTPROTO=static/i'  $int
    grep ^IPADDR  $int &&    sed -i "s/^IPADDR=.*/IPADDR=$hostnameip/g"  $int || echo "IPADDR="$hostnameip >>$int
    sed -i "s/^NETMASK=.*//g" $int
    grep ^PREFIX  $int &&  sed -i "s/^PREFIX=.*/PREFIX=24/g" $int || echo "PREFIX=24" >>$int
    grep ^GATEWAY $int &&  sed -i "s/^GATEWAY=.*/GATEWAY=$(echo $hostnameip|awk -F'.' '{print $1"."$2"."$3}').254/g" $int||echo "GATEWAY="$(echo $hostnameip | awk -F'.' '{print $1"."$2"."$3}')".254" >>$int
    grep ^DNS=  $int &&  sed -i "s/^DNS=.*//g" $int
    grep ^DNS1=  $int &&  sed -i "s/^DNS1=.*/DNS1=100.100.2.136/g" $int || echo "DNS1=100.100.2.136" >>$int
    grep ^DNS2=  $int &&  sed -i "s/^DNS2=.*/DNS2=100.100.2.138/g" $int || echo "DNS2=100.100.2.136" >>$int
    grep ^DNS3=  $int &&  sed -i "s/^DNS3=.*/DNS3=114.114.114.114/g" $int || echo "DNS3=114.114.114.114" >>$int
    grep ^ONBOOT=  $int &&  sed -i "s/^ONBOOT=.*/ONBOOT=yes/g" $int || echo "ONBOOT=yes" >>$int
    cat $int | grep  BOOTPROTO

    #精准匹配除4段的数字
    cat $int | grep --color -o "\([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}"
    #更改主机名
    echo "----------------------------------------------------"
    echo "     (3) 更改主机名"
    hostnamectl set-hostname $hostname
    hostname
    sed -i 's/\\u@\\h\ /\\u@\\H\ /g'   /etc/bashrc
    #yum -y install open-vm-tools
    echo "----------------------------------------------------"
    systemctl restart network
    echo "    (4) 修改网卡名称为eth0"
    mv $(ls /etc/sysconfig/network-scripts/ifcfg-*   | grep -v lo )  /etc/sysconfig/network-scripts/ifcfg-eth0 &> /dev/null
    rm  -f   /etc/udev/rules.d/*.rules
    #sed -i 's/UUID/#UUID/g'   $int
    sed -i 's/NAME=.*/NAME=eth0/g'    /etc/sysconfig/network-scripts/ifcfg-eth0
    sed -i 's/DEVICE=.*/DEVICE=eth0/g'    /etc/sysconfig/network-scripts/ifcfg-eth0
    #关闭IPV6
    grep ^IPV6INIT  /etc/sysconfig/network-scripts/ifcfg-eth0  &&  sed -i "s/^IPV6INIT=yes/IPV6INIT=no/g"  /etc/sysconfig/network-scripts/ifcfg-eth0  ||  echo "IPV6INIT=no" >> /etc/sysconfig/network-scripts/ifcfg-eth0
    sed -i "/^IPV6_/d" /etc/sysconfig/network-scripts/ifcfg-eth0
    systemctl  disable   ip6tables.service
    echo "net.ipv6.conf.all.disable_ipv6=1"  >>/etc/sysctl.conf
    sed  -i   's/GRUB_CMDLINE_LINUX=\"crashkernel/GRUB_CMDLINE_LINUX=\"net.ifnames=0 biosdevname=0\ crashkernel/g' /etc/default/grub
    grub2-mkconfig -o /etc/grub2.cfg   &> /dev/null
    ls /etc/sysconfig/network-scripts/ifcfg-*   | grep -v lo
    echo "----------------------------------------------------"
    echo "    (5) 更新zabbix-Agent"
    #rpm -ivh http://mirrors.aliyun.com/zabbix/zabbix/3.4/rhel/7/x86_64/zabbix-release-3.4-1.el7.centos.noarch.rpm
    #sleep 2
    #yum install zabbix-agent  -y
    #ls /etc/zabbix/zabbix_agentd.conf
    sed -i "s/^Server=.*/Server=$zabbix_server/g"  /etc/zabbix/zabbix_agentd.conf
    sed -i "s/^ServerActive=.*/ServerActive=$zabbix_server/g"  /etc/zabbix/zabbix_agentd.conf
    sed -i "s/^Hostname=.*/Hostname=$hostnameip/g"  /etc/zabbix/zabbix_agentd.conf
    cat /etc/zabbix/zabbix_agentd.conf   | grep ^Server
    cat /etc/zabbix/zabbix_agentd.conf   | grep ^Hostname
    systemctl enable zabbix-agent
    systemctl restart  zabbix-agent
    systemctl status zabbix-agent
    echo "----------------------------------------------------"
    echo "    (6) 配置完成，五秒后重启"
    #read -n 1 -t 30  -p "是否重启主机reboot，y/n? " Number
    echo "----------------------------------------------------"
    echo "$(date)"
    echo "----------------------------------------------------"
#    rm -rf $dir
    echo "rebooting....."
    sleep 5
    #case $Number in
    #[Yy])
    #重启
    #   echo "" >  ./.bash_history   && history -c
#    shutdown -r  now  && exit 0
    #;;
    #esac
    #   ;;
    #   esac
}
